package com.edais.service.imp.gift;


import com.edais.domain.User;
import com.edais.domain.UserMessage;
import com.edais.domain.gift.PointAccount;
import com.edais.domain.gift.PointGrade;
import com.edais.persistence.gift.PointAccountMapper;
import com.edais.persistence.gift.PointGradeMapper;
import com.edais.persistence.gift.PointGradePrivilegeMapper;
import com.edais.service.RateRisesCouponsService;
import com.edais.service.UserMessageService;
import com.edais.service.gift.PointAccountService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

@Service
public class PointAccountServiceImpl implements PointAccountService {
	
	private static final Log logger = LogFactory.getLog(PointAccountServiceImpl.class);
	
	@Autowired
	private PointAccountMapper pointAccountMapper;

    @Autowired
    private PointGradeMapper pointGradeMapper;

    @Autowired
    private PointGradePrivilegeMapper pointGradePrivilegeMapper;

    @Autowired
    private UserMessageService userMessageService;

    @Autowired
    private RateRisesCouponsService rateRisesCouponsService;

    @Override
    public PointAccount findAccount(Long userId, String mobile) {
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("userId",userId);
        params.put("userMobile",mobile);
        return pointAccountMapper.selectPointAccount(params);
    }

    @Override
    public PointAccount findAccount(User user) {
        if(null == user)
            return null;
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("userId",user.getId());
        params.put("userMobile",user.getMobile());
        return pointAccountMapper.selectPointAccount(params);
    }

    @Override
    public PointAccount createAccount(String logInfo, User user, Date nowDate) {
        logger.info(logInfo+"创建积分账户");
        PointAccount account = new PointAccount(user);
        account.setCreateTime(nowDate);
        account.setCreatePer("shopservice");
        pointAccountMapper.insert(account);
        return account;
    }

    @Override
    public void addPointAmount(PointAccount account,Integer amount,Date nowDate){
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("id", account.getId());
        params.put("amount", amount);
        params.put("updateTime", nowDate);
        params.put("updatePer", "shopservice");
        pointAccountMapper.addPointByParams(params);
    }

    @Async
    @Override
    public void pointUpgradeOpt(String logInfo, User user, Integer originalAmount, Integer addAmount) {
        String ownLog = logInfo + "积分等级升级操作-";
        logger.info(ownLog+"开始");

        //验证是否升级
        //原等级
        PointGrade originalPointGrade =pointGradeMapper.getPointGradeByUserPoint(originalAmount);
        //加过积分后的等级
        PointGrade pointGrade =pointGradeMapper.getPointGradeByUserPoint(originalAmount + addAmount);

        ownLog += "原等级"+originalPointGrade.getGrade() + "现等级"+ pointGrade.getGrade()+"-";

        //升级了
        if(originalPointGrade.getIntGrade() < pointGrade.getIntGrade()){
            logger.info(ownLog+"积分等级升级了");
            //做消息插入操作
            logger.info(ownLog+"插入消息开始");
            UserMessage message = pointGrade.toUserMessage(user);
            this.userMessageService.insert(message);
            logger.info(ownLog+"插入消息完成");

            //判断是否做自动签到操作
            int autoSign = pointGradePrivilegeMapper.countAutoSign(pointGrade.getGrade());
            if(autoSign == 1){
                logger.info(ownLog+"自动签到开始");
                userAppSign(ownLog,user);
                logger.info(ownLog+"自动签到完成");
            }else{
                logger.info(ownLog+"无活期自动签到等级特权");
            }
        }else{
            logger.info(ownLog+"积分等级未升级");
        }

        logger.info(ownLog+"完成");
    }

    private void userAppSign(String logInfo ,User user){

        Map<String, Object> paramMap = new LinkedHashMap<String, Object>();
        paramMap.put("userId",user.getId());
        paramMap.put("mobile", user.getMobile());
        paramMap.put("type", 10);

        try{
            Map<String,Object> result = rateRisesCouponsService.addRateRisesCoupons(paramMap);
            logger.info(logInfo+"签到接口返回信息："+result.toString());
        }catch(Exception e){
            logger.error(logInfo+"签到调用失败");
        }
    }
}
